package com.ffcs.crmd.tsp.task.job.impl.test;

import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;

/**
 * 
 * 功能说明:业务处理线程
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年1月6日 下午11:11:39
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
public class BusinessProcess implements Runnable {
    
    private String                      param;
    /**
     * 任务子线程锁(用于移除jobThreadMap)
     */
    private final Lock                  threadLock;
    
    /**
     * 任务锁
     */
    private final CountDownLatch        jobDetailLock;
    
    /**
     * 任务线程缓存标记线程是否都已执行完毕
     */
    private final Map<Runnable, String> jobThreadMap;
    
    public BusinessProcess(String param, Map<Runnable, String> jobThreadMap,
        CountDownLatch jobDetailLock, Lock threadLock) {
        this.param = param;
        this.threadLock = threadLock;
        this.jobDetailLock = jobDetailLock;
        this.jobThreadMap = jobThreadMap;
    }
    
    @Override
    public void run() {
        try {
            System.out.println("----------" + param);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try{
                threadLock.lock();
                try {
                    jobThreadMap.remove(this);
                    if (jobThreadMap.isEmpty()) {
                        jobDetailLock.countDown();
                    }
                } finally {
                    threadLock.unlock();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        
    }
}
